/*
 * @LastEditors: yuSeen
 * @LastEditTime: 2022-01-01 11:16:17
 * 天凉好个秋
 */
import { defineStore } from "pinia";
import { getAccountInfo } from '@/service/api/user/user'
import { useLocalStorage } from '@/hooks/localStorage/useLocalStorage'
import type { IUserStore, IUserStoreState ,IUserStoreAction } from '@/types/store/userStoreType'
const { getStorageItem, setStorageItem } = useLocalStorage()

export const useUserStore = defineStore({
  id: "user",
  state: (): IUserStoreState => ({
    userInfo: {
      nickname: "未登录",
      avatarUrl: '',
      vipType: 0,
    },
    token: getStorageItem("token") || "",
  }),
  getters: {
    // 是否已登录
    isLogin(): boolean {
      return this.token !== "";
    },
  },
  actions:  {
    // 设置token(cookie)
    setToken(token: string) {
      this.token = token
      setStorageItem("token", token)
    },
    // 更新用户信息
    async updateUserInfo() {
      const { account, profile } = await getAccountInfo()
      this.userInfo = Object.assign(account, profile)
    },
    // 退出登录
    logout: async () => {

    },
  },
});
